﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ProLoupanControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        Button BtnSet, btnDelAll, BtnQSXY;
        TextBox showfjnum;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                BindDDl();
            }
            BtnSet = (Button)FindControl("BtnSet");
            BtnSet.Click += new EventHandler(BtnSet_Click);
            Repeater FlourPre = (Repeater)FindControl("FlourPre");
            FlourPre.ItemDataBound += new RepeaterItemEventHandler(FlourPre_ItemDataBound);
            BindData();
            DataTable dt = _EluZSDataProvider.GetProloupan(HttpContext.Current.Request["ZID"]);
            FlourPre.DataSource = dt;
            FlourPre.DataBind();
            
            
            btnDelAll = (Button)FindControl("btnDelAll");
            btnDelAll.Click += new EventHandler(btnDelAll_Click);

            BtnQSXY = (Button)FindControl("BtnQSXY");
            BtnQSXY.Click += new EventHandler(BtnQSXY_Click);
        }

        protected void BtnQSXY_Click(object sender, EventArgs e)
        {
            showfjnum = (TextBox)FindControl("showfjnum");
            string[] num = showfjnum.Text.Split(',');
            if (num.Length > 1)
            {
                List<string> perid = new List<string>() ;
                for (int i = 1; i < num.Length; i++)
                {
                    string dt = "";
                    string sql = @"Select id From zs_zsperson 
                    Where Id  =  
                    (Select zspid From zs_proloupan 
                    Where Id = '" + num[i].ToString() + "' And pid = '" + Page.Request["PID"] + "')";
                    dt = _EluZSDataProvider.GetString(sql);
                    perid.Add(dt);
                }
                for (int i = 0; i < perid.Count-1; i++)
                {
                    if (perid[i].ToString() != "" && perid[i + 1].ToString() != "")
                    {
                        if (perid[i].ToString() != perid[i + 1].ToString())
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('您选择的房间号不是同一人持有，请重新选择!');</script>");
                            showfjnum.Text = "";
                            return;
                        }
                    }
                    else if (perid[i].ToString() == "" || perid[i + 1].ToString() == "")
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('您选择的房间号其中有无产权人的，请检查!');</script>");
                        showfjnum.Text = "";
                        return;
                    }
                }
                string num1 = showfjnum.Text.Substring(1, showfjnum.Text.Length-1);
                string qsid = _EluZSDataProvider.GetSomeStr("zs_cqzhxy", "id", "fid", num1);
                string getsession = _EluZSDataProvider.GetSomeStr("ZS_USER", "TABFS", "ID", userid);
                if (getsession == "0")
                {
                    if (qsid != "")
                    {
                        HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShu.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&ID=" + qsid + "&FID=" + num1 + "&action=edit");
                    }
                    else
                    {
                        HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShu.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&FID=" + num1 + "&action=insert");
                    }
                }
                else if (getsession == "1")
                {
                    if (qsid != "")
                    {
                        HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShuInOne.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&ID=" + qsid + "&FID=" + num1 + "&action=edit");
                    }
                    else
                    {
                        HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShuInOne.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&FID=" + num1 + "&action=insert");
                    }
                }
            }
            else if ( num.Length<=1 )
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),"s2","<script>alert('请选择房间号！');</script>");
            }
            
           
            
        }

        public void btnDelAll_Click(object sender, EventArgs e)
        {
            string strPId = HttpContext.Current.Request["PID"];
            string strZId = HttpContext.Current.Request["ZID"];
            TextBox showagnum = (TextBox)FindControl("showagnum");
            string agnums = showagnum.Text.Trim();
            if (!string.IsNullOrEmpty(agnums))
            {
                string iagnums = string.Empty;
                if (agnums.IndexOf(",单选一层") == 0)
                {
                    iagnums = agnums.Replace(",单选一层", "");
                }
                else
                {
                    iagnums = agnums;
                }
                if (!string.IsNullOrEmpty(iagnums))
                {
                    string[] arrnums = iagnums.Substring(1, iagnums.Length - 1).Split(',');
                    if (arrnums.Length > 0)
                    {
                        for (int i = 0; i < arrnums.Length; i++)
                        {
                            string strnum = arrnums[i];
                            if (!string.IsNullOrEmpty(strnum))
                            {
                                string usql = string.Format(@"update zs_proloupan set status=0 where pid='{0}' 
And zid ='{1}' And 楼层房间号='{2}'", strPId, strZId, strnum);
                                _EluZSDataProvider.RunSqlNon(usql);
                            }
                        }
                        Antu.Utilities.UtilitiesPage.AlertDialog("删除成功！", "window.location.href=location.href;");
                    }

                }
            }
            else
            {
                Antu.Utilities.UtilitiesPage.AlertDialog("请至少选择一个要删除的房间！", "window.location.href=location.href;");
            }

        }

        protected void BtnSet_Click(object sender, EventArgs e)
        {
            DropDownList 房屋类型 = (DropDownList)FindControl("房屋类型");
            DropDownList 房型 = (DropDownList)FindControl("房型");
            TextBox 建筑面积 = (TextBox)FindControl("建筑面积");
            string strPId = HttpContext.Current.Request["PID"];
            string strZId = HttpContext.Current.Request["ZID"];
            TextBox showagnum = (TextBox)FindControl("showagnum");
            string agnums = showagnum.Text.Trim();
            if (!string.IsNullOrEmpty(agnums))
            {
                string iagnums = string.Empty;
                if (agnums.IndexOf(",单选一层") == 0)
                {
                    iagnums = agnums.Replace(",单选一层", "");
                }
                else
                {
                    iagnums = agnums;
                }
                if (!string.IsNullOrEmpty(iagnums))
                {

                    string[] arrnums = iagnums.Substring(1, iagnums.Length - 1).Split(',');
                    if (arrnums.Length > 0)
                    {
                        for (int i = 0; i < arrnums.Length; i++)
                        {
                            string strnum = arrnums[i];
                            if (!string.IsNullOrEmpty(strnum))
                            {
                                string sql = string.Format(@"Select count(*) From zs_proloupan Where pid='{0}' 
And zid ='{1}' And 楼层房间号='{2}'", strPId, strZId, strnum);
                                if (Convert.ToInt32(_EluZSDataProvider.GetString(sql)) > 0)
                                {
                                    string usql = string.Format(@"update zs_proloupan set 房屋类型='{0}',房型='{1}',建筑面积='{2}' where pid='{3}' 
And zid ='{4}' And 楼层房间号='{5}'", 房屋类型.SelectedValue, 房型.SelectedValue, 建筑面积.Text.Trim(), strPId, strZId, strnum);
                                    _EluZSDataProvider.RunSqlNon(usql);
                                }
                            }
                        }

                    }

                }
            }
            Antu.Utilities.UtilitiesPage.AlertDialog("设置成功！", "window.location.href=location.href;");
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string temp = HttpContext.Current.Request["query"];
            string strPid = HttpContext.Current.Request["PID"];
            string strZid = HttpContext.Current.Request["ZID"];
            Button BtnCancel = (Button)FindControl("BtnCancel");
            System.Web.UI.HtmlControls.HtmlTable showop1 = (System.Web.UI.HtmlControls.HtmlTable)FindControl("showop1");
            if (temp == "loupan")
            {
                bool ckdata = _EluZSDataProvider.CheckData(strPid, strZid);
                if (ckdata)
                {
                    showop1.Visible = false;
                }
                else
                {
                    BtnCancel.Visible = false;
                }
            }
        }
        protected void BindData()
        {
            Button createfloor = (Button)FindControl("createfloor");
            Button clear = (Button)FindControl("clear");
            Button BtnCancel = (Button)FindControl("BtnCancel");
            createfloor.Click += new EventHandler(createfloor_Click);
            clear.Click += new EventHandler(clearfloor_Click);
            BtnCancel.Click += new EventHandler(BtnCancel_Click);
        }
        private void Bind()
        {
            Repeater FlourPre = (Repeater)FindControl("FlourPre");
            DataTable dt = _EluZSDataProvider.GetProloupan(HttpContext.Current.Request["ZID"]);
            FlourPre.DataSource = dt;
            FlourPre.DataBind();
        }
        protected void FlourPre_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            Repeater RoomPre = (Repeater)e.Item.FindControl("RoomPre");
            Label flourid = (Label)e.Item.FindControl("flourid");
            RoomPre.ItemDataBound += new RepeaterItemEventHandler(RoomPre_ItemDataBound);
            List<EluZSProvider.ProLoupan> proloupan = _EluZSDataProvider.GetProLoupan(flourid.Text, HttpContext.Current.Request["ZID"]);
            RoomPre.DataSource = proloupan;
            RoomPre.DataBind();

        }
        protected void RoomPre_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
            Literal Literal2 = (Literal)e.Item.FindControl("Literal2");
            Literal Literal3 = (Literal)e.Item.FindControl("Literal3");
            Literal Literal4 = (Literal)e.Item.FindControl("Literal4");
            Literal Literal5 = (Literal)e.Item.FindControl("Literal5");
            Literal Literal6 = (Literal)e.Item.FindControl("Literal6");
            Literal Literal7 = (Literal)e.Item.FindControl("Literal7");
            EluZSProvider.YJCS yjcs = _EluZSDataProvider.GetYJCS();
            string tt = yjcs.安置房源面积小于被征收面积;
            if (tt == "1")
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["PID"]) && !string.IsNullOrEmpty(HttpContext.Current.Request["ZID"]))
                {
                    DataTable dt = _EluZSDataProvider.GetLoupanRoom(HttpContext.Current.Request["PID"], HttpContext.Current.Request["ZID"]);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            string strZSMJ = dt.Rows[i]["建筑面积"].ToString();//安置面积
                            string strAZMJ = dt.Rows[i]["房屋建筑面积"].ToString();//被征收面积
                            string strLCFH = dt.Rows[i]["楼层房间号"].ToString();
                            string strCQR = dt.Rows[i]["产权人"].ToString();
                            string strCZR = dt.Rows[i]["承租人"].ToString();
                            if (string.IsNullOrEmpty(strZSMJ)) { strZSMJ = "0"; }
                            if (string.IsNullOrEmpty(strAZMJ)) { strAZMJ = "0"; }
                            double t1 = Convert.ToDouble(strZSMJ);
                            double t2 = Convert.ToDouble(strAZMJ);
                            if (t1 < t2)
                            {
                                if (Literal6.Text.Trim() == strLCFH)
                                {
                                    Literal7.Text = "<span style='background-color:Red;'>&nbsp;&nbsp;</span>";
                                }
                            }
                            if (Literal6.Text.Trim() == strLCFH)
                            {
                                Literal5.Text = strCQR;
                                if (!string.IsNullOrEmpty(strCZR)) { Literal5.Text = strCZR; }
                            }
                        }
                    }
                }
            }
        }
        protected void createfloor_Click(object sender, EventArgs e)
        {
            TextBox floorcountmin = (TextBox)FindControl("floorcountmin");
            TextBox floorcountmax = (TextBox)FindControl("floorcountmax");
            TextBox floornums = (TextBox)FindControl("floornums");
            TextBox shihao = (TextBox)FindControl("shihao");
            if (string.IsNullOrEmpty(floorcountmin.Text) || string.IsNullOrEmpty(floorcountmax.Text) || string.IsNullOrEmpty(floornums.Text))
            {
                HttpContext.Current.Response.Write("<script>alert('楼层数与房间数不得为空！');history.back();</script>");
                HttpContext.Current.Response.End();
            }
            int isanzhi = 0;
            int floormin = 1;
            int floormax = 1;
            int rooms = 1;
            bool zisnum = true;
            try
            {
                floormin = Convert.ToInt32(floorcountmin.Text);
                floormax = Convert.ToInt32(floorcountmax.Text);
                rooms = Convert.ToInt32(floornums.Text);
            }
            catch
            {
                zisnum = false;
            }
            if (!zisnum)
            {
                HttpContext.Current.Response.Write("<script>alert('楼层数,房间数必须为数字,请更改！');history.back();</script>");
                HttpContext.Current.Response.End();
            }

            int shao = 0;
            if (!string.IsNullOrEmpty(shihao.Text))
            {
                shao = Convert.ToInt32(shihao.Text);
            }
            for (int temp = floormin; temp < floormax + 1; temp++)
            {
                if (shao > 1)
                {
                    for (int index = 1; index < rooms + (shao - 1) + 1; index++)
                    {
                        int nums = _EluZSDataProvider.GetLoupanType("count(*)", HttpContext.Current.Request["ZID"], temp, index);
                        if (nums <= 0)
                        {
                            int status = 0;
                            if (index >= shao)
                            {
                                status = 1;
                            }
                            _EluZSDataProvider.InsertLoupan(isanzhi, HttpContext.Current.Request["PID"], HttpContext.Current.Request["ZID"], temp, index, status);
                        }
                    }
                }
                else
                {
                    for (int index = 1; index < rooms + 1; index++)
                    {
                        int nums = _EluZSDataProvider.GetLoupanType("status", HttpContext.Current.Request["ZID"], temp, index);
                        if (nums <= 0)
                        {
                            int numse = _EluZSDataProvider.GetLoupanType("count(*)", HttpContext.Current.Request["ZID"], temp, index);
                            if (numse > 0)
                            {
                                _EluZSDataProvider.UpdateLoupan("status", HttpContext.Current.Request["ZID"], temp, index);
                            }
                            else
                            {
                                _EluZSDataProvider.InsertLoupan(isanzhi, HttpContext.Current.Request["PID"], HttpContext.Current.Request["ZID"], temp, index, 9);
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write("<script>location.href='ProLoupan.aspx?PID=" + HttpContext.Current.Request["PID"] + "&ZID=" + HttpContext.Current.Request["ZID"] + "&query=" + HttpContext.Current.Request["query"] + "';</script>");
        }
        protected void clearfloor_Click(object sender, EventArgs e)
        {
            bool del = _EluZSDataProvider.DeleteXmByID("zs_proloupan", "ZID", HttpContext.Current.Request["ZID"]);
            HttpContext.Current.Response.Write("<script>location.href='ProLoupan.aspx?PID=" + HttpContext.Current.Request["PID"] + "&ZID=" + HttpContext.Current.Request["ZID"] + "&query=" + HttpContext.Current.Request["query"] + "';</script>");
        }//删除
        private string zhuangid;
        public string ZhuangID
        {
            get { return zhuangid; }
            set { zhuangid = value; }
        }
        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            string pID = HttpContext.Current.Request["PID"];
            string query = HttpContext.Current.Request["query"];
            if (!string.IsNullOrEmpty(pID) && !string.IsNullOrEmpty(query) && query == "cqzh")
            {
                HttpContext.Current.Response.Redirect("ProjectZhuang.aspx?PID=" + pID + "&query=cqzh");
            }
            else if (!string.IsNullOrEmpty(pID) && !string.IsNullOrEmpty(query) && query == "xycx")
            {
                HttpContext.Current.Response.Redirect("CX_XYSJ.aspx");
            }
            else
            {
                HttpContext.Current.Response.Redirect("ProjectZhuang.aspx?PID=" + pID + "&query=loupan");
            }
        }

        public void BindDDl()
        {
            DropDownList ddl房屋类型 = (DropDownList)FindControl("房屋类型");
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("幢房屋类型");
            ddl房屋类型.DataSource = _params;
            ddl房屋类型.DataTextField = "keyname";
            ddl房屋类型.DataValueField = "keyname";
            ddl房屋类型.DataBind();

            DropDownList ddl房型 = (DropDownList)FindControl("房型");
            List<EluZSProvider.Params> _params2 = _EluZSDataProvider.GetParams("房型");
            ddl房型.DataSource = _params2;
            ddl房型.DataTextField = "keyname";
            ddl房型.DataValueField = "keyname";
            ddl房型.DataBind();

        }
    }
}


